/**
 * 下单
 * @author Qasim <15750783791@163.com>
 * @version 2016/9/19
 */
var self = memberInfo = '';

document.addEventListener('plusready' , ready ,  false);

function ready(){
	
	self = plus.webview.currentWebview(); 
	
	$('.title').text(self.title);
	$('.classify').html(self.classify);
	var uid = self.uid , business_id = self.aid;
	
	//下单
	
	$('.add_order').on('click' , function(){
		var name = phone = company = address = invoice = '';
		$('.mui-input-row').each(function(index){
			switch (index){
				case 0:
					name = $(this).find('input').val();
					break;
				case 1:
					phone = $(this).find('input').val();
					break;
				case 2: 
					company = $(this).find('input').val();
				
					break;
				case 3:
					address = $(this).find('input').val();
					break;
				default:
					invoice = $(this).find('input').val();
					break;
			}
		})
		if(!name.trim()){
			toast('联系人姓名不能为空哦');
			return;
		}
		if(!p1.test(phone)){
			toast('手机号码格式有误');
			return;
		}
		if(!company.trim()){
			toast('单位不能为空哦');
			return;
		}
		if(!address.trim()){
			toast('地址不能为空哦');
			return;
		}
		if(!invoice.trim()){
			toast('发票抬头不能为空哦');
			return;
		}
		plus.nativeUI.showWaiting('操作中...');
		$.ajax({
			type:"post",
			url:apiRoot +'/Home/Orders/add_order/',
			data:{
				business_id : self.aid,
				uid: self.uid,
				tid : 35,
				name : name,
				status : 1,
				phone : phone,
				company:company,
				address　:　address,
				invoice　: invoice,
				title : self.nickname +'在' + new Date().toLocaleString() + '生成了一个订单 , 订单号为:'
 			},
			success : function(data){
				plus.nativeUI.closeWaiting();
				if(data > 0){ 
					toast('下单成功');
					//跳转
					plus.nativeUI.showWaiting('跳转中...');
					
					var me_oeder = plus.webview.create('me-oeder.html', 'me_oeder', {}, {});
					
					me_oeder.addEventListener('loaded', function (){
						me_oeder.show('pop-in');
						plus.nativeUI.closeWaiting();
						self.close();
					})
				}
			},
			error : function(e){
				
				console.log(JSON.stringify(e));
				
				plus.nativeUI.closeWaiting();
				
				toast('连接超时,请检查网络');
				
			}
		});
	})
}

